﻿Imports Publics
Imports System.Windows.Forms

Public Class FrmCapNhatThanhVien
    Private STT_int As Integer

    Private _Action As String
    Public Property Action() As String
        Get
            Return _Action
        End Get
        Set(ByVal value As String)
            _Action = value
        End Set
    End Property

    Private _IDThanhVien As String
    Public Property IDThanhVien() As String
        Get
            Return _IDThanhVien
        End Get
        Set(ByVal value As String)
            _IDThanhVien = value
        End Set
    End Property

    Private _objThanhVien As DM_ThanhVien
    Public Property objThanhVien() As DM_ThanhVien
        Get
            Return _objThanhVien
        End Get
        Set(ByVal value As DM_ThanhVien)
            _objThanhVien = value
        End Set
    End Property

    Private Sub FrmCapNhatThanhVien_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            btnThoat_ItemClick(Nothing, Nothing)
        End If
        'Ghi và đóng
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pSAVECLOSE, btnGhiVaDong) Then
            btnGhiVaDong_ItemClick(Nothing, Nothing)
        End If
    End Sub

    Private Sub FrmCapNhatThanhVien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Select Case Action
            Case vAddAction
                txtMaThanhVien.Text = TimMaThanhVien()
                txtCapBac.EditValue = 0
                txtTienThanhToan.EditValue = 0
                txtDiem.EditValue = 0
            Case vEditAction, vCopyAction
                If objThanhVien IsNot Nothing Then
                    txtMaThanhVien.Text = objThanhVien.MaThanhVien
                    txtTenThanhVien.Text = objThanhVien.HoTenThanhVien
                    txtDiaChi.Text = objThanhVien.DiaChi
                    txtNgaySinh.DateTime = objThanhVien.NgaySinh
                    txtSoCMTND.Text = objThanhVien.SoCMT
                    txtNgayCap.DateTime = objThanhVien.NgayCap
                    txtNoiCap.Text = objThanhVien.NoiCap
                    txtTLCK.EditValue = objThanhVien.TiLeCK.GetValueOrDefault

                    If Action = vCopyAction Then
                        txtCapBac.EditValue = 0
                        txtTienThanhToan.EditValue = 0
                        txtDiem.EditValue = 0
                        txtTLCK.EditValue = 0
                    Else
                        txtCapBac.EditValue = objThanhVien.CapBac.GetValueOrDefault
                        txtTienThanhToan.EditValue = objThanhVien.TienThanhToan.GetValueOrDefault
                        txtDiem.EditValue = objThanhVien.Diem.GetValueOrDefault
                        txtTLCK.EditValue = objThanhVien.TiLeCK.GetValueOrDefault
                    End If

                End If
        End Select
    End Sub

    Private Function TimMaThanhVien() As String
        Dim strMaThanhVien As String = ""
        Try
            STT_int = dbTV.Func_GetMaxMaThanhVien().GetValueOrDefault + 1
            strMaThanhVien = "0000000" & STT_int.ToString
        Catch ex As Exception
            Return ""
        End Try
        Return strMaThanhVien.Substring(strMaThanhVien.Length - 6, 6)
    End Function

    Private Function SaveData() As Boolean
        Try
            Dim dteNgay As DateTime = dbTV.GetDateTimeServer

            Select Case Action
                Case vAddAction, vCopyAction
                    Dim objAdd As New DM_ThanhVien
                    objAdd.ID = CreateID()
                    IDThanhVien = objAdd.ID
                    objAdd.MaThanhVien = txtMaThanhVien.Text
                    objAdd.HoTenThanhVien = txtTenThanhVien.Text
                    objAdd.DiaChi = txtDiaChi.Text
                    objAdd.SoDT = txtSoDienThoai.Text
                    objAdd.NgaySinh = txtNgaySinh.DateTime
                    objAdd.SoCMT = txtSoCMTND.Text
                    objAdd.NgayCap = txtNgayCap.DateTime
                    objAdd.NoiCap = txtNoiCap.Text
                    objAdd.CapBac = 0
                    objAdd.TienThanhToan = 0
                    objAdd.Diem = 0
                    objAdd.TiLeCK = CDec("0" & txtTLCK.EditValue)
                    objAdd.STT_Int = STT_int
                    objAdd.InUsed = True
                    objAdd.EditedOn = dteNgay 
                    dbTV.DM_ThanhViens.InsertOnSubmit(objAdd)
                    iSubmitChange()
                Case vEditAction
                    Dim objEdit = (From ds In dbTV.DM_ThanhViens Where ds.InUsed AndAlso ds.ID = objThanhVien.ID Select ds).FirstOrDefault
                    If objEdit IsNot Nothing Then
                        IDThanhVien = objEdit.ID
                        objEdit.MaThanhVien = txtMaThanhVien.Text
                        objEdit.HoTenThanhVien = txtTenThanhVien.Text
                        objEdit.DiaChi = txtDiaChi.Text
                        objEdit.SoDT = txtSoDienThoai.Text
                        objEdit.NgaySinh = txtNgaySinh.DateTime
                        objEdit.SoCMT = txtSoCMTND.Text
                        objEdit.NgayCap = txtNgayCap.DateTime
                        objEdit.NoiCap = txtNoiCap.Text
                        objEdit.TiLeCK = CDec("0" & txtTLCK.EditValue)
                        objEdit.EditedOn = dteNgay
                        iSubmitChange()
                    End If
            End Select
        Catch ex As Exception
            Return False
        End Try
        Return True

    End Function

    Private Function CheckSave() As String
        If String.IsNullOrEmpty(txtMaThanhVien.Text.Trim) Then
            Return "Bạn chưa tạo được mã thành viên"
        End If
        If String.IsNullOrEmpty(txtTenThanhVien.Text.Trim) Then
            Return "Bạn chưa nhập tên thành viên"
        End If
        Select Case Action
            Case vAddAction, vCopyAction
                If dbTV.Func_TrungMaThanhVien(txtMaThanhVien.Text.Trim) Then
                    txtMaThanhVien.Text = TimMaThanhVien()
                End If
            Case vEditAction
                '// K lam gi
        End Select

        Return ""
    End Function
    Private Sub btnGhiVaDong_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnGhiVaDong.ItemClick
        Dim strErr = CheckSave()
        If Not String.IsNullOrEmpty(strErr) Then
            MsgInfo(strErr)
            Return
        End If
        If SaveData() Then
            Me.Close()
        End If
    End Sub

    Private Sub btnThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThoat.ItemClick
        Me.Close()
    End Sub
End Class